package org.csu.corp.util;

import org.apache.commons.collections4.CollectionUtils;
import org.csu.corp.entity.Salary;

import java.util.List;

public class SalaryUtil {
    public static Double calSal(Salary salary) {
        return Double.valueOf(salary.getBasicSalary() + salary.getLunchSalary() + salary.getTrafficSalary() +
                salary.getPensionBase() * salary.getPensionPer() +
                salary.getMedicalBase() * salary.getMedicalPer() +
                salary.getAccumulationFundBase() * salary.getAccumulationFundPer());
    }

    /**
     * 计算每个月的天数
     *
     * @param year 年份
     * @param month 月份
     * @return days 每个月的天数
     */
    public static int getDaysOfMonth(int year, int month)
    {
        int days = 0;

        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
        {
            days = 31;
        }
        else if (month == 4 || month == 6 || month == 9 || month == 11)
        {
            days = 30;
        }
        else
        { // 2月份，闰年29天、平年28天
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
            {
                days = 29;
            }
            else
            {
                days = 28;
            }
        }

        return days;
    }

    public static <T> List<T> splitList(List<T> list, int pageNo, int pageSize) {
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        int totalCount = list.size();
        pageNo = pageNo - 1;
        int fromIndex = pageNo * pageSize;
//        System.out.println("fromIndex" + fromIndex);
        // 分页不能大于总数
        if(fromIndex >= totalCount) {
            return null;
        }
        int toIndex = ((pageNo + 1) * pageSize);
//        System.out.println("toIndex" + toIndex);
        if (toIndex > totalCount) {
            toIndex = totalCount;
        }
        return list.subList(fromIndex, toIndex);
    }
}
